package com.skycode.dao;

import com.skycode.bean.AdminBean;
import com.skycode.util.DBConnection;
import com.skycode.util.Time;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    DBConnection DBConn = new DBConnection();
    private AdminBean adminBean;

    public int CheckLogin(String s1, String s2) throws SQLException {
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = null;
        boolean OK = true;
            String sql = "select * from admin where AdminName = '" + s1 + "' and AdminPwd = '" + s2 + "'";
            rs = stmt.executeQuery(sql);
        if(!rs.next()){
                if (!rs.next()) {
                    OK = false;
                }
                return OK;

            }
        public List<AdminBean> getList(int page) {
            Connection conn = DBConn.getConn();
            String sql = "select * from admin limit "+(page-1)*10+",10";
            List<AdminBean> list = new ArrayList<AdminBean>();
            try {
                Statement statement = conn.createStatement();
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next()) {
                    AdminBean adminBean = new AdminBean();
                    adminBean.setAdminId(rs.getInt("AdminID"));
                    adminBean.setAdminPwd(rs.getString("AdminPwd"));
                    adminBean.setAdminType(rs.getInt("AdminType"));
                    adminBean.setLastLoginTime(rs.getString("LastLoginTime"));
                    adminBean.setAdminName(rs.getString("AdminName"));
                    list.add(adminBean);
                }
            } catch (SQLException e) {
                throw new RuntimeException();
            }
            return list;
        }

        /**
         * 添加新闻
         *
         * @param
         * @return
         */
        public int add(AdminBean admin) {
            Connection conn = DBConn.getConn();
            String sql = "insert into admin value(null,?,?,?,?)";
            try {
                PreparedStatement pmst = conn.prepareStatement(sql);
                pmst.setString(1, admin.getAdminName());
                pmst.setString(2, admin.getAdminPwd());
                pmst.setInt(3, admin.getAdminType());
                pmst.setString(4, (Time.getDate());
                return pmst.executeUpdate(); // 执行添加 返回受影响的行数
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }

        /**
         * 更新功能
         *
         * @param
         * @return
         */
        public int update(AdminBean admin) {
            Connection conn = DBConn.getConn();
            String sql = "update admin set AdminName=?, AdminPwd=?, AdminType=?, LastLoginTime=? where AdminID=?";
            try {
                PreparedStatement pmst = conn.prepareStatement(sql);
                pmst.setString(1, admin.getAdminName());
                pmst.setString(2, admin.getAdminPwd());
                pmst.setInt(3, admin.getAdminType());
                pmst.setString(4, (new Date()).toString());
                pmst.setInt(5, admin.getAdminId());
                System.out.println("执行sql：" + pmst.toString());
                return pmst.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }

        public int delete(int AdminId) {
            Connection conn = DBConn.getConn();
            try {
                String sql = "delete from admin where AdminID=?";
                PreparedStatement pmst = conn.prepareStatement(sql);
                pmst.setInt(1, AdminId);
                return pmst.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
        public int count(){
            Connection conn = DBConn.getConn();
            try {
                String sql = "select count(*) from admin";
                Statement statement = conn.createStatement();
                ResultSet rs = statement.executeQuery(sql);
                if(rs.next()){
                    return rs.getInt(1);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            return 0;
        }
    }